package com.heyiming.ldap.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.ldap.authentication.NullLdapAuthoritiesPopulator;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .ldapAuthentication()
                .userDnPatterns("CN={0},OU=Users,OU=SHG,OU=CN,OU=IT,OU=SNG")
                .ldapAuthoritiesPopulator(new NullLdapAuthoritiesPopulator()) //ldap不管理权限
//                .userSearchBase("DC=ikea,DC=com")             //userSearchBase和userSearchFilter是用匿名登录时查询用的
//                .userSearchFilter("sAMAccountName={0}")
                .contextSource()
                .url("ldap://ikea.com:389/dc=ikea,dc=com");
//                .and()
//                .passwordCompare()  //匿名登录时，查询出来的user 做password比较
//                .passwordEncoder(new BCryptPasswordEncoder())
//                .passwordAttribute("userPassword");
    }
}
